package com.hschinese.pronunciation.fragments;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.adapter.PinyinResultAdapter;
import com.hschinese.pronunciation.bean.TestResultBean;
import com.hschinese.pronunciation.db.DBHelper;
import com.hschinese.pronunciation.utils.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wo on 2015/11/9.
 */
public class PinyinResultFragment extends Fragment{
    private Context mContext;
    private View mView = null;
    private ListView mListView = null;
    private PinyinResultAdapter mAdapter = null;
    private List<TestResultBean> mData = null;

    public PinyinResultFragment() {
        super();
    };

    public PinyinResultFragment(Context cxt) {
        super();
        mContext = cxt;
        mData = new ArrayList<TestResultBean>();
        initData();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.pinyin_result, container,false);
        mListView = (ListView) mView.findViewById(R.id.pinyin_list);
        mAdapter = new PinyinResultAdapter(mData, mContext);
        mListView.setAdapter(mAdapter);
        return mView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void initData() {
        mData.clear();
        // 筛选出拼音的
        String wherearg = " type=? ";
        String values[] = new String[]{"7"};
        Cursor cursor = DBHelper.getInstance(mContext).query(DBHelper.TEST_RESULT_TABLE, wherearg, values);
        // 数据做转换
        while(cursor.moveToNext()) {
            TestResultBean d = new TestResultBean();
            d.setTone(cursor.getString(cursor.getColumnIndex("tone")));
            d.setRightTimes(cursor.getInt(cursor.getColumnIndex("rightTimes")));
            d.setWrongTimes(cursor.getInt(cursor.getColumnIndex("wrongTimes")));
            d.setWrongTones(cursor.getInt(cursor.getColumnIndex("wrongTones")));
            d.setType(cursor.getInt(cursor.getColumnIndex("type")));
            mData.add(d);
            Logger.e("xionghy - mData: " + d.toString());
        }
        cursor.close();
        Logger.e("xionghy - mData size: " + mData.size());
    }

}
